﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using Main.Configs;
using Main.Controls;

namespace Main.Tools
{
    public class BitmapPrinter
    {    
        public static void PrintMultipleImages(string[] imagePaths)
        {
            PrintDialog printDialog = new PrintDialog();
            
            if (printDialog.ShowDialog() == true)
            {


                FixedDocument document = new FixedDocument();
                for (int i = 0; i < imagePaths.Length; i++)
                {
                    string imagePath = imagePaths[i];
                    FixedPage fixedPage = new FixedPage();
                    System.Windows.Controls.Image image = new System.Windows.Controls.Image();
                    image.Source = new BitmapImage(new Uri(imagePath));

                    double x = printDialog.PrintTicket.PageMediaSize.Width.Value;
                    double y = printDialog.PrintTicket.PageMediaSize.Height.Value;
                    if(printDialog.PrintTicket.PageOrientation==System.Printing.PageOrientation.Landscape)
                    {
                        double temp = x;
                        x = y; 
                        y = temp;
                    }

                    image.Width = x; // 
                    image.Height = image.Width*1370/2349; // 
                    fixedPage.Children.Add(image);
                    PageContent pageContent = new PageContent();
                    ((IAddChild)pageContent).AddChild(fixedPage);
                    document.Pages.Add(pageContent);
                }
                //printDialog.PrintVisual()
                printDialog.PrintDocument(document.DocumentPaginator, "Print Images");
            }
        }

        public async static Task PrintImages()
        {
            PrintDialog printDialog = new PrintDialog();

            if (printDialog.ShowDialog() == true)
            {
                printDialog.PrintVisual(new VoucherPrintPaper(),"test");
            }

            return;

                // Configure open file dialog box
            var dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.InitialDirectory = VoucherPrintConfig.Current.OutputFolder;
            dialog.DefaultExt = ".png"; // Default file extension
            dialog.Filter = "凭证图片|*.png"; // Filter files by extension
            dialog.Multiselect = true;

            // Show open file dialog box
            bool? result = dialog.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                PrintMultipleImages(dialog.FileNames);
            }
        }
    }
}
